package com.zw.pdm.module.file.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.FileSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.file.service.FileConfirmService;
import com.zw.pdm.module.file.service.FileListService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划确认接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/file/confirm")
@SuppressWarnings("all")
public class FileConfirmController extends BaseController {

    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private FileConfirmService fileConfirmService;
    @Autowired
    private FileListService fileListService;


    @GetMapping
    @RequiresPermissions(value = {"file:luess:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "file/confirm/fileLssue";
    }

    /**
     * @Author mayongfeng
     * @Date 13:38 2020/12/19
     * @Description 项目确认完成页面在一个页面
     *
     **/
//    @GetMapping("end")
//    @RequiresPermissions(value = {"file:end:view"}, logical = Logical.OR)
//    public String confirmEnd(Model model) {
//        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
//        return "file/confirm/fileConfirmEnd";
//    }
    /** 完结跳转页的方法*/
    @GetMapping("end")
    @RequiresPermissions(value = {"file:end:view"}, logical = Logical.OR)
    public String confirmEnd(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "file/confirm/modifyFileEnd";
    }

    /** 查询审核过后的存档列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions(value = {"file:luess:view"}, logical = Logical.OR)
    public JsonResult list(FileSheet fileSheet) {
        startPage();
        List<FileSheet> list = fileConfirmService.findFileConfirm(fileSheet);
        return jsonResult(list);
    }

    /** 查看页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions(value = {"file:luess:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/filePrint";

    }
    /** 查看页面 */
    @GetMapping("/showPlan")
    @RequiresPermissions(value = {"file:luessQuery:view"}, logical = Logical.OR)
    public String showPlan(String proCode, Model model) {
        model.addAttribute("proCodeId",proCode);
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "file/confirm/fileConfirmPlan";
    }

    /** 下发页面 */
    @GetMapping("/check/{id}")
    @RequiresPermissions(value = {"file:luess:check"}, logical = Logical.OR)
    public String check(    @PathVariable("id") Integer sheetId, Model model) {
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet", fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/confirm/fileConfirm";

    }

    /** 下发到档案室 */
    @ResponseBody
    @PostMapping("/y")
    @RequiresPermissions(value = {"file:luess:check"}, logical = Logical.OR)
    public JsonResult yes(Integer id) {
        return isNotNull(fileConfirmService.confirm(id));
    }


    /** 驳回存档清单 */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions(value = {"file:luess:check"}, logical = Logical.OR)
    public JsonResult reject(FileSheet sheet) {
        return jsonResult(fileConfirmService.fileReject(sheet));
    }


    /** 完结列表 */
    @ResponseBody
    @PostMapping("/endList")
    @RequiresPermissions(value = {"file:end:view"}, logical = Logical.OR)
    public JsonResult endList(FileSheet fileSheet) {
        startPage();
        List<FileSheet> list = fileConfirmService.findEndList(fileSheet);
        return jsonResult(list);
    }

    /** 完结列表按部分分查 */
    @ResponseBody
    @PostMapping("/branchSheet")
    @RequiresPermissions(value = {"file:end:view"}, logical = Logical.OR)
    public JsonResult branchSheet(FileSheet fileSheet) {
        startPage();
        List<FileSheet> list = fileConfirmService.findBrancHSheet(fileSheet);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 9:13 2020/12/21
     * @Description 查看详情明细
     *
     **/
    @GetMapping("planInfo/{id}")
    @RequiresPermissions(value = {"file:end:view"}, logical = Logical.OR)
    public String planInfo(@PathVariable("id") Integer sheetId, Model model) {
        /** 通过Id查询主表和明细表的信息*/
        FileSheet fileSheet = fileListService.getfileListById(sheetId);
        model.addAttribute("fileSheet",fileSheet);
        model.addAttribute("process", fileListService.findVpBySheetId(sheetId));
        model.addAttribute("filePlan", fileListService.findFilePlanBySheetId(sheetId));
        return "file/list/filePrint";
    }

    /**
     * @Author mayongfeng
     * @Date 9:20 2020/12/21
     * @Description 项目完结状态修改
     *
     **/
    @PostMapping("preCommit")
    @RequiresPermissions(value = {"file:end:success"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult preCommit(Integer id){
        return isNotNull(fileConfirmService.updateEndStatusById(id));

    }

    /** 编辑档案备注 */
    @ResponseBody
    @PostMapping("/modify")
//    @RequiresPermissions(value = {"file:sign:view"}, logical = Logical.OR)
    public JsonResult modify(FileSheet fileSheet) {
        return isNotNull(fileConfirmService.modifyFile(fileSheet));
    }





}
